package com.heking.zxinglib;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.zxing.utils.ZxingHelper;

public class MainActivity extends AppCompatActivity {

    private Button mBtn;
    private TextView mMessage;
    private static String[] PERMISSIONS_REQUIRED = new String[]{Manifest.permission.CAMERA};
    private static final int REQUEST_CODE = 1231;
    private ZxingHelper zxingHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        if (allPermissionsGranted()) {
            doAfterPermissionsGranted();
        } else {
            ActivityCompat.requestPermissions(this, PERMISSIONS_REQUIRED, REQUEST_CODE);
        }
    }

    private void findViews() {
        mBtn = findViewById(R.id.btn);
        mMessage = findViewById(R.id.message);

        mBtn.setOnClickListener(v -> {
            zxingHelper = new ZxingHelper(this);
            zxingHelper.setCallback(new ZxingHelper.Callback() {
                @Override
                public void result(int code, String msg) {
                    if (code == ZxingHelper.Callback.CODE_SUCCESS) {
                        mMessage.setText(msg);
                    } else {
                        mMessage.setText(msg);
                    }
                }
            });
            zxingHelper.start();

        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(zxingHelper!=null){
            zxingHelper.onActivityResult(requestCode,resultCode,data);
        }
    }

    private void doAfterPermissionsGranted() {
        Toast.makeText(this, "全部权限", Toast.LENGTH_SHORT).show();
    }

    private boolean allPermissionsGranted() {
        for (String permission : PERMISSIONS_REQUIRED) {
            if (ContextCompat.checkSelfPermission(getBaseContext(), permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            if (allPermissionsGranted()) {
                doAfterPermissionsGranted();
            } else {
                Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

}